import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as kcors from 'kcors';
import * as bodyParser from 'koa-bodyparser';
// DI Container
import containerScope from '../lib/middleware/containerScope';
import createContainer from '../lib/serviceContainer';
// import apiContainer from '../lib/apiContainer';
import responseCalls from '../lib/middleware/responseCalls';
import notFoundHandler from '../lib/middleware/notFoundHandler';


export default async function createServer() {
    const app = new Koa();
    const router = new Router();

    app.use(responseCalls);
    app.use(kcors());
    app.use(bodyParser());
    

    const container = createContainer(router);

  // Adds middleware that creates a new Container Scope for each request.
  app.use(containerScope(container));


  // Install routes
  app.use(router.allowedMethods());
  app.use(router.routes());

  // Default handler when nothing stopped the chain.
  app.use(notFoundHandler);

  return app;

}